package com.wk.service.implement;

import com.wk.dao.Coursedao;
import com.wk.pojo.*;
import com.wk.service.Courseservice;
import com.wk.utils.AliyunOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StreamUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * @program: thesis
 * @description: Course的实现类
 * @author: 王侃
 * @create: 2020-02-15 12:52
 **/
@Service
public class Courseserviceimpl implements Courseservice {

    @Autowired
    Coursedao coursedao;

    @Override
    public List<Coursetype> selectbyalltype() {
        return coursedao.selectbyalltype();
    }

    @Override
    public List<Course_topic> selectbyalltopic() {
        return coursedao.selectbyalltopic();
    }

    @Override
    public String selectbyvideo(Integer id) {
        return coursedao.selectbyvideo(id);
    }

    @Override
    public void showmedia(String path, OutputStream outputStream) {
        try {
            StreamUtils.copy(new FileInputStream(path), outputStream);
        } catch (Exception e) {
        }
    }

    @Override
    public List<Map<String, Object>> selectbyradio(Integer course_id) {
        return coursedao.selectbyradio(course_id);
    }

    @Override
    public Course_exam selectbyid(String id) {
        return coursedao.selectbyid(id);
    }

    @Override
    public String upload(MultipartFile file) {
        try {
            if(null != file){
                String filename = file.getOriginalFilename();
                if(!"".equals(filename.trim())){
                    File newFile = new File(filename);
                    FileOutputStream os = new FileOutputStream(newFile);
                    os.write(file.getBytes());
                    os.close();
                    //上传到OSS，使用工具类提供的upload方法
                    String uploadUrl = AliyunOSSUtil.upload(newFile);
                    System.out.println(uploadUrl);
                    return uploadUrl;
                }
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return "";
    }

    @Async
    //异步操作
    @Override
    public void deletealiyunoss(String path) {
        AliyunOSSUtil.deleteBlog(path);
    }

    @Override
    public void insertintotopic(Course_topic course_topic) {
        coursedao.insertintotopic(course_topic);
    }

    @Override
    public List<Course_topic> selectbytopiclist(Integer user_id,String course_name) {
        return coursedao.selectbytopiclist(user_id,course_name);
    }

    @Override
    public void deletebycourseexam(Integer course_id) {
        coursedao.deletebycourseexam(course_id);
    }

    @Override
    public void deletebycoursetopic(Integer id) {
        coursedao.deletebycoursetopic(id);
    }

    @Override
    public Course_topic selectbytopicall(Integer id) {
        return coursedao.selectbytopicall(id);
    }

    @Override
    public Integer courtbyalluser() {
        return coursedao.courtbyalluser();
    }

    @Override
    public void updatebycoursetopic(Course_topic course_topic) {
        coursedao.updatebycoursetopic(course_topic);
    }

    @Override
    public List<Course_exam> selectbyuser_exam(Integer user_id) {
        return coursedao.selectbyuser_exam(user_id);
    }

    @Async
    @Override
    public void insertintocourse_exam(Course_exam exam) {
        coursedao.insertintocourse_exam(exam);
    }

    @Override
    public void updatebycourseexam(Course_exam exam) {
        coursedao.updatebycourseexam(exam);
    }

    @Override
    public void deletebycoursetestid(Integer id) {
        coursedao.deletebycoursetestid(id);
    }

    @Override
    public void updatecoursepraise(Integer id, Integer praise) {
        coursedao.updatecoursepraise(id,praise);
    }

    @Override
    public List<Course_comment> selectbyidcomment(Integer course_id) {
        return coursedao.selectbyidcomment(course_id);
    }

    @Override
    public String selectbycountcomment(Integer course_id) {
        return coursedao.selectbycountcomment(course_id);
    }

    @Override
    public void insertintocoursecomment(Course_comment comment) {
        coursedao.insertintocoursecomment(comment);
    }

    @Override
    public void updatecommentpraise(Integer id) {
        coursedao.updatecommentpraise(id);
    }

    @Override
    public List<Course_topic> selectbytypecourse(String coursetype_id,String course_name) {
        return coursedao.selectbytypecourse(coursetype_id,course_name);
    }

    @Override
    public List<Course_topic> selectbyallcoursetopic(String course_name) {
        return coursedao.selectbyallcoursetopic(course_name);
    }

    @Override
    public Coursetype selectbycoursetypeid(String id) {
        return coursedao.selectbycoursetypeid(id);
    }

    @Override
    public void updatebyitemtype(Integer id) {
        coursedao.updatebyitemtype(id);
    }

    @Override
    public List<Course_topic> selectbydate_topic(Integer coursetype_id,String course_name) {
        return coursedao.selectbydate_topic(coursetype_id,course_name);
    }

    @Override
    public List<Course_topic> selectbydate_topic_noindex(String course_name) {
        return coursedao.selectbydate_topic_noindex(course_name);
    }

    @Override
    public List<Course_data> selectbyalldatas(Integer course_id) {
        return coursedao.selectbyalldatas(course_id);
    }

    @Override
    public List<Course_data> selectbyuserid_data(Integer user_id) {
        return coursedao.selectbyuserid_data(user_id);
    }

    @Override
    public void insertintocourse_data(Course_data data) {
        coursedao.insertintocourse_data(data);
    }

    @Override
    public Course_data selectbyidcourse_data(Integer id) {
        return coursedao.selectbyidcourse_data(id);
    }

    @Override
    public void updateintopdf(Course_data data) {
        coursedao.updateintopdf(data);
    }

    @Override
    public void deletebyidcourse_data(Integer id) {
        coursedao.deletebyidcourse_data(id);
    }

    @Override
    public void deletebyidcourse_comment(Integer id) {
        coursedao.deletebyidcourse_comment(id);
    }
}
